{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c23b4dd1",
   "metadata": {},
   "source": [
    "# Vertical Chat\n",
    "A sample how to build a chat for small businees using: \n",
    "\n",
    "* GPT 35\n",
    "* Panel\n",
    "* OpenAI \n",
    "\n",
    "Create a .py file to be executed with Streamlt \n",
    "I'm using jupyter just to keep it simple\n",
    "feel free to use you favourite IDE like Pycharm or any other. \n",
    "\n",
    "*Note: This is a sample notebook, if the list of items is too large you can reach the max tokens in the OpenAI API. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1d00720",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install openai\n",
    "!pip install panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a03f026a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai \n",
    "import panel as pn\n",
    "from mykeys import openai_api_key\n",
    "openai.api_key=openai_api_key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77eac86d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def continue_conversation(messages, temperature=0):\n",
    "    response = openai.ChatCompletion.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=messages,\n",
    "        temperature=temperature, \n",
    "    )\n",
    "    #print(str(response.choices[0].message[\"content\"]))\n",
    "    return response.choices[0].message[\"content\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51bec475",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_prompts_conversation(_):\n",
    "    #Get the value introduced by the user\n",
    "    prompt = client_prompt.value_input\n",
    "    client_prompt.value = ''\n",
    "    \n",
    "    #Append to the context the User promnopt. \n",
    "    context.append({'role':'user', 'content':f\"{prompt}\"})\n",
    "    \n",
    "    #Get the response. \n",
    "    response = continue_conversation(context) \n",
    "    \n",
    "    #Add the response to the context. \n",
    "    context.append({'role':'assistant', 'content':f\"{response}\"})\n",
    "    \n",
    "    #Undate the panels to shjow the conversation. \n",
    "    panels.append(\n",
    "        pn.Row('User:', pn.pane.Markdown(prompt, width=600)))\n",
    "    panels.append(\n",
    "        pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))\n",
    " \n",
    "    return pn.Column(*panels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "922f8d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "context = [ {'role':'system', 'content':\"\"\"\n",
    "Act as an OrderBot, you work collecting orders in a delivery only fast food restaurant called \n",
    "My Dear Frankfurt. \\\n",
    "First welcome the customer, in a very friedly way, then collects the order. \\\n",
    "You wait to collect the entire order, beverages included \\\n",
    "then summarize it and check for a final \\\n",
    "time if everithing is ok or the customer wants to add anything else. \\\n",
    "Finally you collect the payment.\\\n",
    "Make sure to clarify all options, extras and sizes to uniquely \\\n",
    "identify the item from the menu.\\\n",
    "You respond in a short, very friendly style. \\\n",
    "The menu includes \\\n",
    "burguer  12.95, 10.00, 7.00 \\\n",
    "frankfurt   10.95, 9.25, 6.50 \\\n",
    "sandwich   11.95, 9.75, 6.75 \\\n",
    "fries 4.50, 3.50 \\\n",
    "salad 7.25 \\\n",
    "Toppings: \\\n",
    "extra cheese 2.00, \\\n",
    "mushrooms 1.50 \\\n",
    "martra sausage 3.00 \\\n",
    "canadian bacon 3.50 \\\n",
    "romesco sauce 1.50 \\\n",
    "peppers 1.00 \\\n",
    "Drinks: \\\n",
    "coke 3.00, 2.00, 1.00 \\\n",
    "sprite 3.00, 2.00, 1.00 \\\n",
    "vichy catalan 5.00 \\\n",
    "\"\"\"} ]  \n",
    "\n",
    "#Creamos el panel. \n",
    "pn.extension()\n",
    "\n",
    "panels = [] \n",
    "\n",
    "client_prompt = pn.widgets.TextInput(value=\"Hi\", placeholder='Enter text here…')\n",
    "button_conversation = pn.widgets.Button(name=\"talk\")\n",
    "\n",
    "interactive_conversation = pn.bind(add_prompts_conversation, button_conversation)\n",
    "\n",
    "dashboard = pn.Column(\n",
    "    client_prompt,\n",
    "    pn.Row(button_conversation),\n",
    "    pn.panel(interactive_conversation, loading_indicator=True, height=300),\n",
    ")\n",
    "\n",
    "dashboard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88db4691",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
